function [time,value] = lpcvd_Load_SinkToFileDetail(filename,time_to_report)
% [time,value] = lpcvd_Load_SinkToFileDetail(filename) return a TIME
% vector and matrix VALUE. Each column is one run and each row corresponds
% to one time point.
% [time,value] = lpcvd_Load_SinkToFileDetail(filename,time_to_report) 
% return a value at time points specified by TIME_TO_REPORT
formatstring = '%f %f';
fid = fopen(filename);
if fid < 3
    error('Invalid file: %s\n',filename);
end
data = textscan(fid,formatstring);
time_temp = data{1};
value_temp = data{2};
for i = 1:length(time_temp)
    if(time_temp(i+1)<time_temp(i))
        break;
    end
end
num_records_per_run = i;
num_repeats = length(time_temp)/num_records_per_run;
if nargin == 1
    time = time_temp(1:num_records_per_run);
    value = zeros(num_records_per_run,num_repeats);
    for j = 1:num_repeats
        value(:,j) = value_temp((j-1)*num_records_per_run+1:j*num_records_per_run);
    end
elseif nargin == 2
    time = time_to_report;
    value = zeros(length(time),num_repeats);
    for j = 1:num_repeats
        time_OneRun = time_temp((j-1)*num_records_per_run+1:j*num_records_per_run);
        value_OneRun = value_temp((j-1)*num_records_per_run+1:j*num_records_per_run);
        value(:,j) = value_OneRun(time_OneRun==time);
    end
end
